#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/types.h>  
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
	int sockfd;
	sockfd=socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0)
	{
		perror("socket err.");
		return -1;
	}
	struct sockaddr_in serveraddr;
    serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr=inet_addr(argv[1]);

	if(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))< 0)
	{
		perror("connect err.");
		return -1;
	}
	char buf[64];
	ssize_t recvbyte;
	pid_t pid=fork();
	if(pid < 0)
	{
		perror("fork err.");
		return -1;
	}else if(pid == 0)
	{
		while(1)
		{
			memset(buf,0,64);
			recvbyte=recv(sockfd,buf,64,0);
			if(recvbyte < 0)
			{
				perror("recv err.");
			}
			printf("buf:%s\n",buf);

		} 
	}
	else
	{
	//	signal(SIGINT,handler);
		while(1)
		{
			fgets(buf,64,stdin);
			if(buf[strlen(buf)-1] == '\n')
				buf[strlen(buf)-1]='\0';
			send(sockfd,buf,sizeof(buf),0);
		}
		wait(NULL);
	}
	close(sockfd);
	return 0;
}

/*void handler(int sig)
{
	if(sig==SIGINT)
		kill(pid,SIGKILL);
}*/
